package com.amphora.web.filter;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.apache.log4j.Logger;

import com.amphora.util.repository.JPAUtil;

@WebFilter("/EntityManagerFilter")
public class EntityManagerFilter implements Filter {

	Logger logger = Logger.getLogger(JPAUtil.class);
	
	public void destroy() {
		JPAUtil.removeInstance();
		JPAUtil.close();
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		EntityManager em = null;
		try{
			JPAUtil.setEntityManager();
			em = JPAUtil.getEntityManager();
			
			chain.doFilter(request, response);
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
		JPAUtil.init();
	}

}
